﻿using Hangfire;
using Hangfire.Dashboard.BasicAuthorization;
using Hangfire.MySql.Core;
using Microsoft.Extensions.Configuration;
using Tingjian.gas.service.Configurations;
using Tingjian.gas.service.Consts;
using Volo.Abp;
using Volo.Abp.BackgroundJobs.Hangfire;
using Volo.Abp.Modularity;

namespace Tingjian.gas.service.BackgroundJobs
{
    [DependsOn(typeof(AbpBackgroundJobsHangfireModule))]
    public class serviceBackgroundJobsModule : AbpModule
    {

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            //context.Services.AddHangfireServer(options => { 

            //    options.
            //});


            context.Services.AddHangfire(config =>
            {
                var tablePrefix = serviceConsts.DbTablePrefix + "hangfire";

                config.UseStorage(
                            new MySqlStorage(AppSettings.MysqlDbConnStr,
                            new MySqlStorageOptions
                            {
                                TablePrefix = tablePrefix
                            }));
            });
        }

        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();

            //app.UseHangfireServer();

            app.UseHangfireDashboard(options: new DashboardOptions
            {
                Authorization = new[]
                {
                    new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
                    {
                        RequireSsl = false,
                        SslRedirect = false,
                        LoginCaseSensitive = true,
                        Users = new []
                        {
                            new BasicAuthAuthorizationUser
                            {
                                Login = AppSettings.Hangfire.Login,
                                PasswordClear =  AppSettings.Hangfire.Password
                            }
                        }
                    })
                },
                DashboardTitle = "gas service 任务调度中心"
            });

            var service = context.ServiceProvider;

            service.UseRedisListConsumer();

        }

    }
}
